package com.coderly.onlinegallerysharingplatform.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.coderly.onlinegallerysharingplatform.common.constant.SpaceUserPermissionConstant;
import com.coderly.onlinegallerysharingplatform.common.convention.errorcode.ErrorCode;
import com.coderly.onlinegallerysharingplatform.common.convention.exception.BizException;
import com.coderly.onlinegallerysharingplatform.common.response.Response;
import com.coderly.onlinegallerysharingplatform.model.dao.entity.SpaceUserDO;
import com.coderly.onlinegallerysharingplatform.model.dto.req.spaceuser.AddSpaceUserReqDTO;
import com.coderly.onlinegallerysharingplatform.model.dto.req.spaceuser.DeleteSpaceUserReqDTO;
import com.coderly.onlinegallerysharingplatform.model.dto.req.spaceuser.EditSpaceUserReqDTO;
import com.coderly.onlinegallerysharingplatform.model.dto.req.spaceuser.GetSpaceUserReqDTO;
import com.coderly.onlinegallerysharingplatform.model.dto.resp.spaceuser.SpaceUserRespDTO;
import com.coderly.onlinegallerysharingplatform.service.SpaceUserService;
import com.coderly.onlinegallerysharingplatform.service.UserService;
import com.coderly.onlinegallerysharingplatform.toolkit.ExceptionTrowUtils;
import com.coderly.onlinegallerysharingplatform.toolkit.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class SpaceUserController {

    @Resource
    private SpaceUserService spaceUserService;

    @Resource
    private UserService userService;

    /**
     * 添加成员到空间
     */
    @PostMapping("/space_user/add")
    @SaCheckPermission(type = "space", value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public Response<Long> addSpaceUser(@RequestBody AddSpaceUserReqDTO addSpaceUserReqDTO) {
        ExceptionTrowUtils.throwIf(addSpaceUserReqDTO == null, ErrorCode.PARAMS_ERROR);
        long id = spaceUserService.addSpaceUser(addSpaceUserReqDTO);
        return Response.success(id);
    }

    /**
     * 从空间移除成员
     */
    @PostMapping("/space_user/delete")
    @SaCheckPermission(type = "space", value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public Response<Boolean> deleteSpaceUser(@RequestBody DeleteSpaceUserReqDTO deleteSpaceUserReqDTO) {
        if (deleteSpaceUserReqDTO == null || deleteSpaceUserReqDTO.getId() <= 0) {
            throw new BizException(ErrorCode.PARAMS_ERROR);
        }
        return spaceUserService.deleteSpaceUser(deleteSpaceUserReqDTO);
    }

    /**
     * 查询某个成员在某个空间的信息
     */
    @PostMapping("/space_user/get")
    @SaCheckPermission(type = "space", value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public Response<SpaceUserDO> getSpaceUser(@RequestBody GetSpaceUserReqDTO getSpaceUserReqDTO) {
        // 参数校验
        ExceptionTrowUtils.throwIf(getSpaceUserReqDTO == null, ErrorCode.PARAMS_ERROR);
        Long spaceId = getSpaceUserReqDTO.getSpaceId();
        Long userId = getSpaceUserReqDTO.getUserId();
        ExceptionTrowUtils.throwIf(ObjectUtil.hasEmpty(spaceId, userId), ErrorCode.PARAMS_ERROR);
        // 查询数据库
        SpaceUserDO spaceUserDO = spaceUserService.getOne(spaceUserService.buildSpaceUserQueryWrapper(getSpaceUserReqDTO));
        ExceptionTrowUtils.throwIf(spaceUserDO == null, ErrorCode.NOT_FOUND_ERROR);
        return Response.success(spaceUserDO);
    }

    /**
     * 查询成员信息列表
     */
    @PostMapping("/space_user/list")
    @SaCheckPermission(type = "space", value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public Response<List<SpaceUserRespDTO>> listSpaceUser(@RequestBody GetSpaceUserReqDTO getSpaceUserReqDTO) {
        ExceptionTrowUtils.throwIf(getSpaceUserReqDTO == null, ErrorCode.PARAMS_ERROR);
        List<SpaceUserDO> spaceUserList = spaceUserService.list(
                spaceUserService.buildSpaceUserQueryWrapper(getSpaceUserReqDTO)
        );
        return Response.success(spaceUserService.getSpaceUserRespDTOList(spaceUserList));
    }

    /**
     * 编辑成员信息（设置权限）
     */
    @PostMapping("/space_user/edit")
    @SaCheckPermission(type = "space", value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public Response<Boolean> editSpaceUser(@RequestBody EditSpaceUserReqDTO editSpaceUserReqDTO) {
        if (editSpaceUserReqDTO == null || editSpaceUserReqDTO.getId() <= 0) {
            throw new BizException(ErrorCode.PARAMS_ERROR);
        }
        return spaceUserService.editSpaceUser(editSpaceUserReqDTO);
    }

    /**
     * 查询我加入的团队空间列表
     */
    @PostMapping("/space_user/list/my")
    public Response<List<SpaceUserRespDTO>> listMyTeamSpace() {
        Long loginUserId = UserUtil.getLoginUserId();
        GetSpaceUserReqDTO getSpaceUserReqDTO = new GetSpaceUserReqDTO();
        getSpaceUserReqDTO.setUserId(loginUserId);
        List<SpaceUserDO> spaceUserList = spaceUserService.list(
                spaceUserService.buildSpaceUserQueryWrapper(getSpaceUserReqDTO)
        );
        return Response.success(spaceUserService.getSpaceUserRespDTOList(spaceUserList));
    }
}
